<template>
    <div>
        <el-form
            :rules="rules"
            ref="loginFormRef"
            :model="loginForm"
            @keydown.enter="loginHandler"
            class="loginContainer"
        >
            <h3 class="loginTitle">登录页面</h3>
            <el-form-item prop="userName">
                <el-input type="text" auto-complete="false" v-model="loginForm.userName" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password" auto-complete="false" v-model="loginForm.password" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item prop="code">
                <el-input type="text" auto-complete="false" v-model="loginForm.code" placeholder="请输入验证码" style="width: 250px;margin-right: 5px"></el-input>
                <img :src="captchaUrl" @click="refreshCaptchaHandle">
            </el-form-item>
            <el-checkbox v-model="checked" class="rememberMe">记住我</el-checkbox>
            <el-button type="primary" style="width:100%" @click="loginHandler">登录</el-button>
        </el-form>
    </div>
</template>

<script>
    import {ElMessage} from "element-plus";
    import router from '../router';

    export default {
        name: "Login",
        data(){
            return {
                captchaUrl:'',
                checked:false,
                loginForm:{
                    userName:'admin',
                    password:'admin0123',
                    code:''
                },
                rules:{
                    userName:[{required:true, message:'用户名必填',trigger:'blur'}],
                    password:[{required:true, message:'密码是必填',trigger:'blur'}],
                    code:[{required:true, message:'验证码是必填',trigger:'blur'}],
                }
            };
        },
        methods:{
            loginHandler(){
                this.$refs["loginFormRef"].validate(valid=>{
                    if(valid){
                        ElMessage({
                            message:'登录成功',
                            type:'success'
                        });
                        sessionStorage.setItem('tokenStr', '123');
                        router.replace('/home');
                    }else{
                        ElMessage.error("请填写必填字段");
                    }
                });
            },
            refreshCaptchaHandle(){

            }
        }
    }
</script>

<style scoped>
    .loginContainer{
        border-radius: 15px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 380px;
        padding: 15px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }
    .loginTitle{
        margin: 0px auto 40px auto;
        text-align: center;
    }

    .rememberMe{
        text-align: left;
        margin: 0px 0px 15px 8px;
    }
</style>
